﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.ParkManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.ParkManage.Jop
{
    public class JobPostingService : IJobPostingService
    {
        //招聘发布
        private readonly IRepository<JobPosting> _repository;

        public JobPostingService(IRepository<JobPosting> repository)
        {
            _repository = repository;
        }

        public async Task<Response<bool>> DeleteByIdJobPostingAsync(JobPosting input)
        {
            var result = await _repository.DeleteByIdAsync(input.MapTo<JobPosting>());

            return Response<bool>.Ok(result > 0);
        }

        public async Task<Response<List<JobPosting>>> GetJobPostingAsync(jops jop)
        {
            var obj = await _repository.GetAllListAsync();


            if (!string.IsNullOrWhiteSpace(jop.publisher))
            {
                obj = obj.Where(x => x.Publisher.Contains(jop.publisher)).ToList();
            }

            if (jop.station.HasValue && jop.enddate.HasValue)
            {
                obj = obj.Where(x => x.CreationTime > jop.station && x.CreationTime < jop.enddate).ToList();
            }

            //总条数
            int total = obj.Count();
            //分页
            obj = obj.Skip((jop.pageIndex - 1) * jop.pageSize).Take(jop.pageSize).ToList();

            var result = obj.MapToList<JobPosting>();

            return Response<List<JobPosting>>.Ok(result, total);
        }

        public async Task<Response<bool>> PostJobPosting(JobPosting input)
        {
            var result = await _repository.InsertAsyncById(input.MapTo<JobPosting>());

            return Response<bool>.Ok(result > 0);
        }

        public Task<Response<bool>> PutJobPosting1(JobPosting input)
        {
            input.IsDelete = 2;
            return PutJobPosting(input);
        }

        public Task<Response<bool>> PutJobPosting2(JobPosting input)
        {
            input.IsDelete = 3;
            return PutJobPosting(input);
        }

        public async Task<Response<bool>> PutJobPosting(JobPosting input)
        {
            var result = _repository.UpdateAsync(input.MapTo<JobPosting>());

            return Response<bool>.Ok(result.Result > 0);
        }
    }

    public class jops
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
        public string? publisher { get; set; }
        public DateTime? station { get; set; }
        public DateTime? enddate { get; set; }
    }
}
